package com.qiyu.service.impl;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiyu.common.ServiceException;
import com.qiyu.dao.FirstLevelMenuMapper;
import com.qiyu.entity.FirstLevelMenu;
import com.qiyu.model.request.backend.FirstLevelMenuReq;
import com.qiyu.service.FirstLevelMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


/**
 * 一级菜单
 *
 * @author zhangweiwei
 * @date 2020-07-30
 */
@Slf4j
@Service
public class FirstLevelMenuServiceImpl extends ServiceImpl<FirstLevelMenuMapper, FirstLevelMenu> implements FirstLevelMenuService {

    @Override
    public List<FirstLevelMenu> queryAll() {
        QueryWrapper<FirstLevelMenu> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .orderByAsc(FirstLevelMenu::getSort)
                .orderByDesc(FirstLevelMenu::getUpdateTime);
        List<FirstLevelMenu> firstLevelMenus = this.list(wrapper);

        return firstLevelMenus;
    }

    @Override
    public FirstLevelMenu saves(FirstLevelMenuReq req) {

        if(StringUtils.isEmpty(req.getHandler())){
            throw new ServiceException("请传入handler");
        }
        FirstLevelMenu firstLevelMenu = new FirstLevelMenu();

        switch (req.getHandler()){
            case "query":
                firstLevelMenu = this.query(req);
                break;
            case "update":
                firstLevelMenu = this.update(req);
                break;
            case "add":
                firstLevelMenu = this.add(req);
                break;
            case "del":
                firstLevelMenu  = this.del(req);
                break;
            default:
                throw new ServiceException("handler 传值不正确");
        }

        return firstLevelMenu;
    }

    @Override
    public void online(Boolean online, Long[] ids) {
        if(StringUtils.isEmpty(online)){
            throw new ServiceException("请传入online的值 ：false 或者 true");
        }
        if(StringUtils.isEmpty(ids)){
            throw new ServiceException("请传入上下线的id");
        }
        for(Long id:ids){
            FirstLevelMenu firstLevelMenu = this.getById(id);
            firstLevelMenu.setOnline(online);
            saveOrUpdate(firstLevelMenu);
        }
    }

    @Override
    public void top(String handler, Long id) {
        if(StringUtils.isEmpty(handler)){
            throw new ServiceException("请传入handler");
        }
        if(StringUtils.isEmpty(id)){
            throw new ServiceException("请传入Id");
        }
        QueryWrapper<FirstLevelMenu> wrapper = new QueryWrapper<>();
        FirstLevelMenu firstLevelMenu = this.getById(id);
        Integer curSort = firstLevelMenu.getSort();
        switch (handler){
            case "top": //置顶

                firstLevelMenu.setSort(1);

                break;
            case "foot": //置底
                wrapper.lambda().orderByDesc(FirstLevelMenu::getSort).last("limit 1");
                Integer minSort = this.getOne(wrapper).getSort();
                firstLevelMenu.setSort(++minSort);
                break;
            case "up": //上一个
                if(curSort>1){
                    firstLevelMenu.setSort(--curSort);
                }
                break;
            case "down": //下一个
                firstLevelMenu.setSort(++curSort);
                break;
                default:
                    throw new ServiceException("handler传值不正确");
        }
        saveOrUpdate(firstLevelMenu);
    }



    /**
     * 删除
     * @param req
     * @return
     */
    private FirstLevelMenu del(FirstLevelMenuReq req) {
        FirstLevelMenu firstLevelMenu = this.query(req);
       this.checkFirstLevelMenu(firstLevelMenu);
       this.removeById(req.getId());
        return this.query(req);

    }
    private void checkFirstLevelMenu(FirstLevelMenu firstLevelMenu){
        if(firstLevelMenu == null){
            throw new ServiceException("未查找到相关一级菜单");
        }
    }

    /**
     * 添加
     * @param req
     * @return
     */
    private FirstLevelMenu add(FirstLevelMenuReq req) {
        QueryWrapper<FirstLevelMenu> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(FirstLevelMenu::getSort).last("limit 1");
        //获取最大sort
        FirstLevelMenu menu = this.getOne(wrapper);
        Integer maxSort = 1;
        if(menu != null){
            maxSort = menu.getSort();
            ++maxSort;
        }
        FirstLevelMenu firstLevelMenu = new FirstLevelMenu();
        BeanUtils.copyProperties(req,firstLevelMenu);
        //设置排序
        firstLevelMenu.setSort(maxSort);
        //id设置为空
        firstLevelMenu.setId(null);
        firstLevelMenu.setMenuKey(UUID.randomUUID().toString());
        saveOrUpdate(firstLevelMenu);

       //返回最新添加的数据
       wrapper.lambda().orderByDesc(FirstLevelMenu::getCreateTime);
        firstLevelMenu  = this.getOne(wrapper);
        return firstLevelMenu;
    }

    /**
     * 查询
     * @param req
     * @return
     */
    private FirstLevelMenu query(FirstLevelMenuReq req){
        this.checkId(req);
        FirstLevelMenu firstLevelMenu =  this.getById(req.getId());
        return  firstLevelMenu;
    }

    /**
     * 修改
     * @param req
     * @return
     */
    private FirstLevelMenu update(FirstLevelMenuReq req){
        FirstLevelMenu firstLevelMenu = this.query(req);
        this.checkFirstLevelMenu(firstLevelMenu);
        BeanUtils.copyProperties(req,firstLevelMenu);
        saveOrUpdate(firstLevelMenu);
        return this.query(req);

    }

    /**
     * 校验Id
     * @param req
     */
    private void checkId(FirstLevelMenuReq req){
        if(StringUtils.isEmpty(req.getId()) || req.getId() == 0){
            throw new ServiceException("请传入id");
        }
    }
    

    
}